<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- <div class="box">
            <span>11111</span>
        </div> -->
        <!-- 通过js对象来描述这个节点 -->
    </div>
    <script>
        // 虚拟dom就是一个描述真实dom节点的对象
        // 对象会对dom节点进行描述，无论vue还是react 都是数据驱动视图，不需要获取节点设置节点
        var app1 = {
            type: "div",
            props: {
                class: "box",
                children: [{
                    type: "span",
                    props: {
                        children: ["11111"]
                    }
                }]
            }
        }
        var obj = {
            name: "zhansgan"
        }
        console.log(obj['name']);

        function render(jsx, root) {
            // 创建节点，追加节点，设置属性

            let createElement = (jsx, parent) => {
                console.log(jsx, parent);
                let cur = null; // 这个变量用于承接创建出来的dom节点
                let {
                    type,
                    props
                } = jsx;
                if (typeof (jsx) === "string") {
                    cur = document.createTextNode(jsx)
                    parent.append(cur)
                } else {
                    cur = document.createElement(type)
                    parent.append(cur)
                    console.log(Object.keys(props)); // Object.keys将对象的键转化为数组
                    Object.keys(props).forEach((item, index) => {
                        if (item === "class") {
                            cur.setAttribute('class', props[item])
                        } else if (item === "children") {
                            if (Array.isArray(props[item])) {
                                props[item].map((val => {
                                    createElement(val, cur)
                                }))
                            }
                        }
                    })
                }

            }
            createElement(jsx, root)
        }
        render(app1, app) // 将app1这个对象渲染在id名为app的div里
    </script>
</body>

</html>